<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style type="text/css">
        .div1{
            margin-top: 20px;
            font-size: 16px;
            width: 250px;
        }
    </style>
</head>
<body>
<div style="padding-left: 5%;padding-top:5%">
    <form class="layui-form" method="" action="" style="margin-top: 30px;">
        <input type="hidden" name="id">
        <!--第一个-->
        <div style="float: left;">
            <!--员工姓名-->
            <div class="layui-form-item">
                <label class="layui-form-label">用户账号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_account" lay-verify="required|user_account" autocomplete="off" placeholder="请输入员工姓名" class="layui-input">
                </div>
                <label class="layui-form-label">用户名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_name" lay-verify="required|user_name" autocomplete="off" placeholder="请输入员工姓名" class="layui-input">
                </div>
            </div>
                <div class="layui-form-item">
                <label class="layui-form-label">用户角色：</label>
                <div class="layui-input-inline">
                    <div class="layui-input-inline">
                        <select name="role_id" class="layui-select" lay-verify="required" lay-filter="aihao">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                    <label class="layui-form-label">性别：</label>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="radio" name="sex" value="男" title="男" checked="checked">
                            <input type="radio" name="sex" value="女" title="女">
                        </div>
                    </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户邮箱：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_email" lay-verify="required|user_email" autocomplete="off" placeholder="请输入员工姓名" class="layui-input">
                </div>
                <label class="layui-form-label">使用状态：</label>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="radio" name="user_state" value="1" title="启用" checked>
                        <input type="radio" name="user_state" value="2" title="冻结" >
                    </div>
                </div>
            </div>
            <!--出生日期、文化程度-->
            <div class="layui-form-item">
                <label class="layui-form-label">入职日期：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="entry_time" id="entry_time">
                </div>
                <label class="layui-form-label">离职日期：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="quit_time" id="quit_time">
                </div>
            </div>
            <!--提交按钮-->
            <div class="layui-form-item" style="padding-left: 130px;padding-top: 20px;">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-filter="edit_user" lay-submit="edit_user" style="margin-right: 50px;">修改</button>
                    <button type="button" class="layui-btn layui-btn-normal" id="quit">取消</button>
                </div>
            </div>
        </div>
    </form>
    </div>
</body>
<script src="../../js/jquery-3.4.1.js"></script>
<script rel="script" src="../../layui/layui.js"></script>
<!--<script rel="script" src="../../layui/layui.all.js"></script>-->
<script>
    layui.use(['form','upload','laydate','layer'], function() {
           var  laydate = layui.laydate
            , layedit = layui.layedit
            ,layer=layui.layer
            , form = layui.form;
        form.render();
        //执行一个laydate实例
        laydate.render({
            elem: '#entry_time' //指定元素
        });
        laydate.render({
            elem: '#quit_time' //指定元素
        });
        $.ajax({
            url:"/queryRoleAll.do",
            type:"get",
            dataType:"json",
            success:function (data) {
                console.log(data);
                if (data.code == 0){
                    $.each(data.data, function (index, value) {
                        $('select[name=role_id]').append(new Option(value.role_name,value.role_id));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                }
            }
        });

        //自定义验证规则
        form.verify({
            user_account: function (value) {
                if (value.length < 6) {
                    return '用户名称至少得6个字符';
                }
            },
            user_name: function (value) {
                if (value.length < 2) {
                    return '用户名称至少得2个字符';
                }
            },
            user_email: function (value) {
                var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                if (!reg.test(value)) {
                    return '邮箱格式不符';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });
        form.on('submit(edit_user)', function(data) {
            console.log(data.field);
            $.ajax({
                url:"/userUpdate.do",
                type:"post",
                data:JSON.stringify(data.field),
                dataType:"json",
                success:function (res) {
                    if (res.code == 200){
                        event.preventDefault();
                        layer.msg(res.msg,{icon:1,time:2000});
                        var second=1;
                        //定时器2秒后跳转
                        var interval = setInterval(function () {
                            second--;
                            if (second === -1) {
                                parent.layer.close(parent.layer.index);
                                clearInterval(interval);
                            }
                        }, 1000);
                    }
                    else if (res.code == 500) {
                        layer.msg(res.msg,{icon:5});
                    }else if (res.code == 502) {
                        layer.msg(res.msg,{icon:5});
                    }else if (res.code == 405){
                        layer.msg(res.msg,{icon:5});
                    }
                }
            });
        });
        $(function () {
            $("#quit").click(function () {
                parent.layer.close(parent.layer.index);
            })
        });
    });


</script>
</html>